using Circus.CodeEditor;

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace Circus.Syntax.Lexer
{
	public class LexReswordSet
	{
		private LexStyle reswordStyle;
		private IList<string> reswords;
		private Hashtable reswordsHash;
		private bool caseSensitive;
		private LexSyntaxBlock block;
		private string name = string.Empty;
		[Description("Gets or set name for the \"LexReswordSet\".")]
		public virtual string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				if (this.name != value)
				{
					this.name = value;
					this.OnNameChanged();
				}
			}
		}
		[Description("Gets or sets an \"ILexStyle\" of resword collection.")]
		public virtual LexStyle ReswordStyle
		{
			get
			{
				return this.reswordStyle;
			}
			set
			{
				if (this.reswordStyle != value)
				{
					this.reswordStyle = value;
					this.OnReswordStyleChanged();
				}
			}
		}
		public virtual bool CaseSensitive
		{
			get
			{
				return this.block != null && this.block.CaseSensitive;
			}
			set
			{
				if (this.caseSensitive != value)
				{
					this.caseSensitive = value;
					this.OnCaseSensitiveChanged();
				}
			}
		}
		[Description("Represents index of the \"LexReswordSet\" within the reswords collection.")]
		public virtual int Index
		{
			get
			{
				return (this.block != null) ? this.block.ReswordSets.IndexOf(this) : -1;
			}
		}
		public LexSyntaxBlock Block
		{
			get
			{
				return this.block;
			}
			set
			{
				if (this.block != value)
				{
					this.block = value;
					this.OnBlockChanged();
				}
			}
		}
		[Description("Represents a list of regular expression patterns for the \">LexReswordSet.Reswords\".")]
		public IList<string> Reswords
		{
			get
			{
				return this.reswords;
			}
			set
			{
				this.reswords.Clear();
				foreach (string current in value)
				{
					this.reswords.Add(current);
				}
				this.OnReswordsChanged();
			}
		}
	
		private void BuildReswordHash()
		{
			this.reswordsHash.Clear();
			foreach (string current in this.reswords)
			{
				string text = this.CaseSensitive ? current : current.ToLower();
				this.reswordsHash[text] = text;
			}
		}
		protected virtual void OnBlockChanged()
		{
			this.CaseSensitive = this.block.CaseSensitive;
		}
		protected void OnCaseSensitiveChanged()
		{
			this.BuildReswordHash();
		}
		protected virtual void OnNameChanged()
		{
		}
		protected virtual void OnReswordStyleChanged()
		{
		}
		protected virtual void OnReswordsChanged()
		{
			this.BuildReswordHash();
		}
		public LexReswordSet(LexSyntaxBlock block)
		{
			this.block = block;
			this.caseSensitive = (block != null && block.CaseSensitive);
			this.reswords = new List<string>();
			this.reswordsHash = new Hashtable();
		}
		public virtual int AddResword(string resword)
		{
			this.reswords.Add(resword);
			string text = this.CaseSensitive ? resword : resword.ToLower();
			if (!this.reswordsHash.Contains(text))
			{
				this.reswordsHash.Add(text, text);
			}
			return this.reswords.Count - 1;
		}
		public virtual bool FindResword(string resword)
		{
			return this.reswordsHash.Contains(this.CaseSensitive ? resword : resword.ToLower());
		}
		public virtual void Clear()
		{
			this.reswords.Clear();
			this.reswordsHash.Clear();
		}
	}
}
